﻿using AutoMapper;
using MediatR;
using SmartCity.BasicData.Api.Application.Commond.ProfessionalGrid;
using SmartCity.BasicData.Common;
using SmartCity.BasicData.Domain;
using SmartCity.BasicData.Repo;

namespace SmartCity.BasicData.Api.Application.Handler.ProfessionalGrid
{
    public class QueryProfessionalGridHandler : IRequestHandler<QueryPrefessionalCommand, ApiResult<List<PrefessionalDTO>>>
    {
        private readonly IBaseRepository<ProfessionalGridModel> professRepo;
        private readonly IMapper mapper;

        public QueryProfessionalGridHandler(IBaseRepository<ProfessionalGridModel> professRepo, IMapper mapper)
        {
            this.professRepo = professRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<PrefessionalDTO>>> Handle(QueryPrefessionalCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<PrefessionalDTO>> result = new ApiResult<List<PrefessionalDTO>>();

            var query = professRepo.QueryAllAsync().Result;
            var list = mapper.Map<List<PrefessionalDTO>>(query);

            result.Data = list;
            result.Code = Common.Menus.EnumResultCode.Success;
            result.Msg = "查询成功"; 

            return Task.FromResult(result);
        }
    }
}
